package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.bumptech.glide.Glide;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MeCarActivity extends BaseActivity {

    @BindView(R.id.activity_me_car_imageView)
    ImageView imageView;
    @BindView(R.id.activity_me_car_textView)
    TextView textView;
    @BindView(R.id.activity_me_car_linearLayout)
    LinearLayout linearLayout;
    @BindView(R.id.activity_me_car_no)
    TextView no;
    @BindView(R.id.toolber_color_title)
    TextView title;
    @BindView(R.id.toolber_color_right)
    TextView right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_car);
        ButterKnife.bind(this);

        initView();
        initData();
    }

    private void initData() {
        if (BcUtils.getUserInfo().getBindingElectricBicycle() == null || BcUtils.getUserInfo().getBindingElectricBicycle().equals("")) {
            linearLayout.setVisibility(View.GONE);
            no.setVisibility(View.VISIBLE);
            return;
        }
        StringRequest request = new StringRequest(Request.Method.POST, Constants.getrentcarinfobybindingelectricbicycle, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                Log.i("我的车辆", s);
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        String data = json.getString("data");
                        String usedType = "";
                        switch (new JSONObject(data).getString("used_type")) {
                            case "0":
                                usedType = "租赁";
                                break;
                            case "1":
                                usedType = "新车出售";
                                break;
                            case "2":
                                usedType = "二手车出售";
                                break;
                        }
                        Glide.with(MeCarActivity.this).load(new JSONObject(data).getString("bicyclePictureUrl")).into(imageView);
                        textView.setText("车辆名称：" + new JSONObject(data).getString("bicycle_name") +
                                "\n车辆编码：" + new JSONObject(data).getString("bicycle_code") +
                                "\n车辆价格：¥" + new JSONObject(data).getString("bicycle_price") +
                                "\n使用电池：" + new JSONObject(data).getString("bicycle_battery") +
                                "\n使用类型：" + usedType +
                                "\n车辆描述：" + new JSONObject(data).getString("bicycle_describe") +
                                "\n车辆租金：¥" + new JSONObject(data).getString("bicycle_rent") +
                                "\n车辆押金：¥" + new JSONObject(data).getString("bicycle_deposit") +
                                "\n使用天数：" + new JSONObject(data).getString("bicycle_use_days") +
                                "天\n租金是否可退：" + (new JSONObject(data).getString("bicycle_refund_flg").equals("0") ? "不可退" : "可退") +
                                "\n租车电话：" + new JSONObject(data).getString("sell_phone") +
                                "\n租车地址：" + new JSONObject(data).getString("bicycle_extract_address"));
                    } else {
                        linearLayout.setVisibility(View.GONE);
                        no.setVisibility(View.VISIBLE);
                        toast(msg);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    linearLayout.setVisibility(View.GONE);
                    no.setVisibility(View.VISIBLE);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                VolleyErrorUtils.checkError(volleyError);
                linearLayout.setVisibility(View.GONE);
                no.setVisibility(View.VISIBLE);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("bindingElectricBicycle", BcUtils.getUserInfo().getBindingElectricBicycle());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));

                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

    private void initView() {
        title.setText("我的车辆");
        right.setText("租车记录");
        right.setVisibility(View.VISIBLE);
    }

    @OnClick({R.id.toolber_color_back, R.id.toolber_color_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                finish();
                break;
            case R.id.toolber_color_right:
                Intent intent = new Intent(this, MeBatteryRecordActivity.class);
                intent.putExtra("type", "4");
                startActivity(intent);
                break;
        }
    }
}
